package com.kennedy.springboot.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Component;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import java.util.concurrent.TimeUnit;

/**
 * Created by tao.zeng on 2017/7/31.
 */
@Component
public class Recever {
    @Autowired
    private JmsTemplate jmsTemplate;

    @JmsListener(destination = "my-destination")
    public void receiveMessage(String message) {
        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("接收到：" + message);
    }

    /**
     * 向默认队列发送消息
     */
    public void sendMessage(final String msg) {
        jmsTemplate.send("my-destination", new MessageCreator() {
            public Message createMessage(Session session) throws JMSException {
                return session.createTextMessage(msg);
            }
        });


    }
}
